<?php
/**
 * tpCommunity [a web Community based ThinkPHP5]
 *
 * @author    yuan1994 <tianpian0805@gmail.com>
 * @link      http://tpCommunity.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license   http://www.apache.org/licenses/LICENSE-2.0
 */

namespace app\cgi\logic;

use think\Db;
use think\Config;

class CommunityNode
{
    /**
     * 首页列表生成菜单项
     */
    public function getMenu($uid=0) {
        $prefix = Config::get("database.prefix");
        $sql = "SELECT node.id,node.name,node.pid,node.group_id,node.title,node.type from "
            . "{$prefix}community_role AS role,"
            . "{$prefix}community_role_user AS user,"
            . "{$prefix}community_access AS access ,"
            . "{$prefix}community_node AS node "
            . "WHERE user.user_id='" . $uid . "' "
            . "AND user.role_id=role.id "
            . "AND access.role_id=role.id "
            . "AND role.status=1 "
            . "AND access.node_id=node.id "
            . "AND node.status=1 "
            . "AND node.group_id > 0 "
            . "ORDER BY node.sort ASC";
        return Db::query($sql);
    }
    /**
     * 获取指定控制器的权限
     */
    public function getControllerMenu($uid,$menus='0') {

        $prefix = Config::get("database.prefix");
        $sql = "SELECT node.id,node.name,node.pid,node.group_id,node.title,node.type from "
            . "{$prefix}community_role AS role,"
            . "{$prefix}community_role_user AS user,"
            . "{$prefix}community_access AS access ,"
            . "{$prefix}community_node AS node "
            . "WHERE user.user_id='" . $uid . "' "
            . "AND user.role_id=role.id "
            . "AND access.role_id=role.id "
            . "AND role.status=1 "
            . "AND access.node_id=node.id "
            . "AND node.status=1 "
            . "AND node.pid IN($menus) GROUP BY node.id "
            . "ORDER BY node.sort ASC";
        return Db::query($sql);
    }
}